package com.edu.edu.week_project01.activity.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.edu.edu.week_project01.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SearchActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText et_search;
    private Button bt_search;
    private Button bt_back;
    private View convertview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initView();
    }

    private void initView() {
        GridView mGv = (GridView) findViewById(R.id.search_gv);
        bt_back = (Button) findViewById(R.id.search_bt_back);
        bt_search = (Button) findViewById(R.id.seatch_bt_search);
        et_search = (EditText) findViewById(R.id.search_et);
        bt_back.setOnClickListener(this);
        bt_search.setOnClickListener(this);
        et_search.setOnClickListener(this);
        //当没有数据时显示为空
        mGv.setEmptyView(findViewById(R.id.search_emptyContent));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.search_bt_back://返回
                Toast.makeText(SearchActivity.this,"返回了",Toast.LENGTH_LONG).show();
                finish();
                break;
            case R.id.search_et://输入框搜索
                //点击弹出popwindow
                showPopWindow(view);
                break;
            case R.id.seatch_bt_search://右侧搜索
                //获取搜索内容
                String content = et_search.getText().toString().trim();
                Toast.makeText(SearchActivity.this,"搜索主播",Toast.LENGTH_LONG).show();

                break;
        }
    }

    private void showPopWindow(View view) {
        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertview = layoutInflater.inflate(R.layout.popwindow_list, null);
        PopupWindow mPopWindow = new PopupWindow(convertview,770,270);
        // 使其聚集
        //mPopWindow.setFocusable(true);
        // 设置允许在外点击消失
        mPopWindow.setOutsideTouchable(true);
        mPopWindow.showAsDropDown(convertview, Gravity.TOP,0,0);
        ListView pLv = (ListView)convertview.findViewById(R.id.poput_list_lv);
        int[] imagesId = getImagesId();
        String[] text = getContent();
        List<Map<String,Object>> data = new ArrayList<>();
        for (int i = 0; i <text.length ; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("text", text[i]);
            map.put("images", imagesId[0]);
            data.add(map);
        }
        assert pLv !=null;
        SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.popupwindow_list_item,new String[]{ "text","images"},new int[]{R.id.poput_list_item_tv,R.id.popup_list_item_bt});
        pLv.setAdapter(adapter);
    }

    public int[] getImagesId() {
        return new int[]{R.mipmap.delete_item_history};
    }

    public String[] getContent() {
        return new String[]{"sfdsfafd","dsfaafds","sfsfdsfa","sfdsfafd","dsfaafds","sfsfdsfa"};
    }
}
